<?php

namespace Biz\model\brand;

use Doraemon\model\Phone as PhoneModel;
use Doraemon\service\Member\PhoneCrypto;
use Doraemon\tools\Log;
use hhz\constant\basic\UserExtendPhoneScene;

/**
 *
 * @author huining
 *
 */
class ShopAddres extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_shop';

    const STATUS_NORMAL = 1;

    const STATUS_DELETE = 9;


    //添加城市信息
    public static function addAddressInfo($data)
    {
        if ($data['phone']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($data['phone'], UserExtendPhoneScene::BRAND_SHOP_INFO_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$data['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $data['phone_encrypt_id'] = $encryptPhoneId;
            $data['phone'] = PhoneCrypto::mask($data['phone']);
        } else {
            $data['phone_encrypt_id'] = 0;
            $data['phone'] = '';
        }
        return self::insert($data)->rowcount();

    }

    //根据店铺id信息获取信息
    public static function getAddressByCityBrandId($brand_id)
    {
        $map = [];
        $map['AND'] = [
            'brand_id' => $brand_id,
            'city_status' => self::STATUS_NORMAL
        ];
        $map['ORDER'] = ['city_sort' => 'DESC'];
        return self::select("*", $map);
    }

    //更新
    public static function updateById($id, $data)
    {
        return self::updateByWhere(['id' => $id], $data);
    }

    //根据条件更新
    public static function updateByWhere($where, $data)
    {
        if ($data['phone']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($data['phone'], UserExtendPhoneScene::BRAND_SHOP_INFO_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$data['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $data['phone_encrypt_id'] = $encryptPhoneId;
            $data['phone'] = PhoneCrypto::mask($data['phone']);
        }
        return self::update($data, $where)->rowCount();
    }

    //获取数据
    public static function getDataBywhere($where)
    {
        return self::select('*', $where);
    }


    //获取计数
    public static function getShopCounterByMap($map)
    {
        return self::count($map);
    }

}

